package cc.angis.xiugallery.provider;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import cc.angis.xiugallery.data.FolderData;

public class SystemGalleryProvider implements IFolderSourceProvider{
	
	private Context context;
	
	private List<FolderData> folderDataList;
	
	public SystemGalleryProvider(Context context){
		this.context = context;
		folderDataList = new ArrayList<FolderData>();
	}

	@Override
	public int getSize() {
		return folderDataList.size();
	}

	@Override
	public void initSource() {
		folderDataList.clear();
		String[] projection = new String[]{
	            MediaStore.Images.Media._ID,
	            MediaStore.Images.Media.BUCKET_ID,
	            MediaStore.Images.Media.BUCKET_DISPLAY_NAME
	    };

	    // Get the base URI for the People table in the Contacts content provider.
	    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

	    // Make the query.
	    Cursor cur = context.getContentResolver().query(images,
	            projection, // Which columns to return
	            "1=1" + ")" + "group by" + "(" + MediaStore.Images.Media.BUCKET_ID,         // Which rows to return (all rows)
	            null,       // Selection arguments (none)
	            MediaStore.Images.Media.BUCKET_ID         // Ordering
	            );

	    Log.i("ListingImages"," query count="+cur.getCount());

	    if (cur.moveToFirst()) {
	    	Long imageId;
	        String bucketId;
	        String bucketName;
	        int buckeIdColumn = cur.getColumnIndex(
		            MediaStore.Images.Media.BUCKET_ID);
	        int bucketNameColumn = cur.getColumnIndex(
	            MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

	        int imageIdColumn = cur.getColumnIndex(
	            MediaStore.Images.Media._ID);

	        do {
	            // Get the field values
	        	imageId = cur.getLong(imageIdColumn);
	        	bucketId = cur.getString(buckeIdColumn);
	        	bucketName = cur.getString(bucketNameColumn);

	        	FolderData folderData = new FolderData(bucketId, bucketName);
	        	folderData.addThumbnail(imageId);
	        	folderDataList.add(folderData);
	            // Do something with the values.
//	            Log.i("ListingImages", " bucket=" + bucket 
//	                   + "  date_taken=" + date);
	        } while (cur.moveToNext());

	    }
	}
	
	
	@Override
	public List<FolderData> getFolderList() {
		return folderDataList;		
	}

	@Override
	public Object getItem(int num) {
		// TODO Auto-generated method stub
		return folderDataList.get(num).getId();
	}

	@Override
	public int getType() {
		return 1;
	}
	
	@Override
	public void addFolderData(FolderData folderData) {
		folderDataList.add(0, folderData);
		
	}

	@Override
	public String getItemText(int num) {
		return folderDataList.get(num).getName();
	}

	@Override
	public Object getImageItem(int num) {
		if (folderDataList.get(num).getThumbnailList() != null && folderDataList.get(num).getThumbnailList().size() > 0)
			return folderDataList.get(num).getThumbnailList().get(0);
		else 
			return null;
	}

	@Override
	public void removeFolderData(int position) {
		// TODO Auto-generated method stub
		
	}
	
}
